<?php

class RatsSrcTest extends UnitTestCase {
    function TestRatsClientlib() {
        $this->UnitTestCase("Rats Src Tests");
    }

    // # we don't use the regular setUp and tearDown functions here, cause this would cause too much traffic
    // # instead we're relying on the fact that methods are executed in declaration order
    // function testSetup() {
    //   try {
    //     $rats = new Rats(SUPERUSER, SUPERPWD, BASE);
    //     $newUser = $rats->createUser(USER, EMAIL, PWD);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
    
    function testUploadSrc() {
      try {
        # upload a src
        $rats = new Rats(USER, PWD, BASE);
        $src_entry = $rats->createSrc(SRC_FILENAME, LOCAL_SRC_FILE);
        $this->assertEqual($src_entry->content->params->filename->text, SRC_FILENAME);
        # retrieve the upload src metadata
        $src2_entry = $rats->getSrcEntry($src_entry->id->text);
        $this->assertEqual($src_entry->content->params->filename->text, $src2_entry->content->params->filename->text);
        # retrieve the upload src metadata using the filename as input
        $src3_entry = $rats->getSrcEntryFromFilename(SRC_FILENAME);
        $this->assertEqual($src2_entry->content->params->filename->text, $src3_entry->content->params->filename->text);
        # delete the uploaded src
        $rats->deleteSrc($src_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    function testUploadSameNamedSrcs() {
      try {
        # upload a src
        $rats = new Rats(USER, PWD, BASE);
        $src_entry = $rats->createSrc(SRC_FILENAME, LOCAL_SRC_FILE);
        $this->assertEqual($src_entry->content->params->filename->text, SRC_FILENAME);
        # upload a second src with the same name -> rats should accept and rename
        $src2_entry = $rats->createSrc(SRC_FILENAME, LOCAL_SRC_FILE);
        $this->assertNotEqual($src2_entry->content->params->filename->text, SRC_FILENAME);
        # delete the 1st uploaded src
        $rats->deleteSrc($src_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
        # delete the 2nd uploaded src
        $rats->deleteSrc($src2_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }    

    function testSrcFeed() {
      try {
        # upload a src
        $rats = new Rats(USER, PWD, BASE);
        $src_entry = $rats->createSrc(SRC_FILENAME, LOCAL_SRC_FILE);
        $src2_entry = $rats->createSrc(SRC_FILENAME2, LOCAL_SRC_FILE);
        # get the feed
        $feed = $rats->getSrcFeed();
        $entry_counter = 0;
        foreach ($feed as $entry) {
          switch($entry->id->text) {
            case $src_entry->id->text:
              $entry_counter++;
              break;
            case $src2_entry->id->text:
              $entry_counter++;
              break;
            default:
              assert(False);
          }
        }
        $this->assertEqual($entry_counter, 2);
        # delete the 1st uploaded src
        $rats->deleteSrc($src_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
        # delete the 2nd uploaded src
        $rats->deleteSrc($src2_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }
    
    function testValidSrcURL() {
      try {
        # upload a src
        $rats = new Rats(USER, PWD, BASE);
        $src_entry = $rats->createSrc("my_de.li-cious_ice-cream000000_.21-5.mp4", LOCAL_SRC_FILE);
        $this->assertEqual($src_entry->content->params->filename->text, "my_de.li-cious_ice-cream000000_.21-5.mp4"  );
        # retrieve the upload src metadata
        $src2_entry = $rats->getSrcEntry($src_entry->id->text);
        $this->assertEqual($src_entry->content->params->filename->text, $src2_entry->content->params->filename->text);
        # retrieve the upload src metadata using the filename as input
        $src3_entry = $rats->getSrcEntryFromFilename("my_de.li-cious_ice-cream000000_.21-5.mp4");
        $this->assertEqual($src2_entry->content->params->filename->text, $src3_entry->content->params->filename->text);
        # delete the uploaded src
        $rats->deleteSrc($src_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }
    
    function testInvalidSrcURL() {
      $status = 0;
      try {
        # upload a src
        $rats = new Rats(USER, PWD, BASE);
        $src_entry = $rats->createSrc("what=not_where.mp4", LOCAL_SRC_FILE);
        assert(False);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 400); 
    }


    // 
    // function testTeardown() {
    //   try {
    //     $rats = new Rats(SUPERUSER, SUPERPWD, BASE);
    //     $newUser = $rats->deleteUser(USER);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
}
